package com.zgl.modules.goods.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zgl.modules.goods.DTO.GoodsDTO;
import com.zgl.modules.goods.entity.GoodsEntity;
import com.zgl.modules.goods.mapper.GoodsMapper;
import com.zgl.modules.goods.service.GoodsService;
import com.zgl.modules.order.DTO.OrderDTO;
import com.zgl.modules.order.entity.OrderEntity;
import com.zgl.utils.Params;
import com.zgl.utils.R;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Resource
    GoodsMapper goodsMapper;

    public R savaOne(GoodsEntity goodsEntity) {
        if (1 == goodsMapper.insert(goodsEntity)) {
            return R.success();
        } else {
            return R.error("操作失败");
        }
    }

    public R removeOne(Integer id) {
        if (1 == goodsMapper.deleteById(id)) {
            return R.success();
        } else {
            return R.error("操作失败");
        }
    }

    public R modifyOne(GoodsEntity goodsEntity) {
        if (goodsMapper.updateById(goodsEntity) == 1) {
            return R.success();
        } else {
            return R.error();
        }

    }

    public R getOne(Integer id) {
        GoodsEntity goodsEntity = goodsMapper.selectById(id);
        return R.success(goodsEntity);
    }

    @Override
    public R queryGoodsByname(String name) {
        QueryWrapper<GoodsEntity> queryWrapper = new QueryWrapper<>() ;
        queryWrapper.like("goods_name",name);
        GoodsEntity goodsEntity=goodsMapper.selectOne(queryWrapper);
        return R.success(goodsEntity);
    }

    @Override
    public R querGoodsAsc(GoodsDTO goodsDTO) {
        int total=0;
        QueryWrapper<GoodsEntity> queryWrapper = new QueryWrapper<>();
        List<GoodsEntity> goodsEntityList = goodsMapper.selectList(queryWrapper);
       int currentPage= goodsDTO.getCurrentPage().equals(null)?1:goodsDTO.getCurrentPage();
       int pageSize = goodsDTO.getSize().equals(null)?10:goodsDTO.getSize();
       List<GoodsEntity>goodsDTOS=new ArrayList<>();
        if (goodsEntityList!=null) {
            total=goodsEntityList.size();
            int startRow = 0;
            int endRow = 0;

            if (total != 0) {
                startRow = (currentPage - 1) * pageSize + 1;
                endRow = currentPage * pageSize;
                if (endRow > total) {
                    endRow = total;
                }
                for (int i = startRow - 1; i < endRow; i++) {
                    goodsDTOS.add(goodsEntityList.get(i));
                }
            }
        }
        Map<String,Object> page=new HashMap<>();
        page.put("CurrentPage",currentPage);
        page.put("total",total);
        page.put("goods",goodsDTOS);
        return R.success(page);
    }

    @Override
    public R querGoodsDesc() {
        return null;
    }
    public R querGoodsByName(GoodsDTO goodsDTO){
        QueryWrapper<GoodsEntity> queryWrapper = new QueryWrapper<>() ;
        queryWrapper.and(wq -> {
            wq.and(wq0 -> wq0.like("goods_name", goodsDTO.getGoodsName()));
        });
//        queryWrapper.like("goods_name",orderDTO.getGoodsName()).eq("userid",tokenUserId).eq("delete_status",0);
        List<GoodsEntity> goodsEntityList=goodsMapper.selectList(queryWrapper);
        int currentPage= goodsDTO.getCurrentPage().equals(null)?1:goodsDTO.getCurrentPage();
        int pageSize =goodsDTO.getSize().equals(null)?10:goodsDTO.getSize();
        List<GoodsEntity>ordersDTOS=new ArrayList<>();
        int total=0;
        if (goodsEntityList!=null) {
            total=goodsEntityList.size();
            int startRow = 0;
            int endRow = 0;

            if (total != 0) {
                startRow = (currentPage - 1) * pageSize + 1;
                endRow = currentPage * pageSize;
                if (endRow > total) {
                    endRow = total;
                }
                for (int i = startRow - 1; i < endRow; i++) {
                    ordersDTOS.add(goodsEntityList.get(i));
                }
            }
        }
        Map<String,Object> page=new HashMap<>();
        page.put("CurrentPage",currentPage);
        page.put("total",total);
        page.put("goods",ordersDTOS);
        return R.success(page);
    }
}
